﻿var addressid; var deliverid; var payid; var deliverinfo;var sendid;
var addressobject = null; var deliveobject = null; var payobject = null;

$(function () {
    var _id = $(".addresslist div").attr("data-id");

    $(".addressadd").click(function () {
        window.location = "/MyKunlun/AddressAdd?url=/MyKunlun/Purchase"
    });


    //选择地址
    $(".uladdress").live("click", function () {
        var delive = $(this).attr("delive-data")
        addressid = $(this).attr("data-id")
        $(addressobject).css("background-color", "Black");
        $(this).css("background-color", "Green");
        addressobject = this;
        createDeliver(delive);

    })
    //选择配送方式；
    $(".uldelive").live("click", function () {
        deliverid = $(this).attr("map");
        $(deliveobject).css("background-color", "White")
        $(this).css("background-color", "Black")
        deliveobject = this;
        var m = $(this).attr("map").split("|");
        if (m[3] == 0) {
            $('#pay-3').fadeOut('slow');
        }
        else {
            $('#pay-3').fadeIn('slow');
        }
        $("#pay-1").trigger("click");
        payid = "1";
        sendid = $(this).attr("data-id");
        $("#DeliverPrice").html(deliverid.split("|")[2])
    });

    //选择支付方式
    $(".ulpay").live("click", function () {
        $(payobject).css("background-color", "White")
        $(this).css("background-color", "Black")
        payobject = this;
        payid = $(this).attr("data-id");
        if (payid == 3) {
            $("#DeliverPrice").html(parseFloat(deliverid.split("|")[4]))
        }
        else {
            $("#DeliverPrice").html(parseFloat(deliverid.split("|")[2]))
        }
    });
    $("#ul-" + _id).trigger("click");

    $("#getInvoice").click(function () {
        if ($("#invoiceTxt").css("display") == "none") {
            $("#invoiceTxt").fadeIn('slow');
        }
        else { $("#invoiceTxt").fadeOut('slow'); }
    });
    $("#saveorder").click(function () {

        var Invoice = $("#Invoice").val()
        
       
            
            $.post("/MyKunlun/CreateOrder", { "addressid": addressid, "sendid": sendid, "payid": payid, "invoice": Invoice },
            function (data) {

            })

       

    });
});
//修改配送方式
function createDeliver(deliver) {
    deliverid = "";
    var d = deliver.split(",");
    for (i = 0; i < d.length; i++) {
        var m = d[i].split("|");
        $('#deliver-' + m[0]).removeAttr("map");
        $('#deliver-' + m[0]).attr("map", d[i]);
        if (m[1] == 0) {
            $('#deliver-' + m[0]).fadeOut('slow');
        }
        else {
            $('#deliver-' + m[0]).fadeIn('slow');
            //设置默认配送方式
            if (deliverid == "") {
                $("#deliver-" + m[0]).trigger("click");
            }
        }       
    }
}